package com.zyht.wechat.message;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.zyht.common.util.HttpUtils;

/**
 * 描述：消息发送<br>
 * 作者：ruibo <br>
 * 修改日期：2017年3月5日-下午5:25:02 <br>
 */
public enum CustomerMessageSender {

	/**
	 * 文本 添加如下可以用指定客服号发送消息，customservice":{"kf_account": "test1@kftest"}
	 */
	text {

		@Override
		public final void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "text");
			Map<String, Object> textMap = new HashMap<String, Object>();
			textMap.put("content", params[0]);
			jsonMap.put("text", textMap);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[文本]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	},

	/**
	 * 语音
	 */
	voice {
		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "voice");
			Map<String, Object> voice = new HashMap<String, Object>();
			voice.put("media_id", params[0]);
			jsonMap.put("voice", voice);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[语音]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	},
	/**
	 * 图片
	 */
	image {
		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "image");
			Map<String, Object> image = new HashMap<String, Object>();
			image.put("media_id", params[0]);
			jsonMap.put("image", image);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[图片]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	},

	/**
	 * 图文，内容跳转详情
	 */
	article {

		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "mpnews");
			Map<String, Object> mpnews = new HashMap<String, Object>();
			mpnews.put("media_id", params[0]);
			jsonMap.put("mpnews", mpnews);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[图文详情]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	},

	/**
	 * 视频
	 */
	video {

		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "video");
			Map<String, Object> video = new HashMap<String, Object>();
			video.put("media_id", params[0]);
			video.put("thumb_media_id", params[1]);
			video.put("title", params[2]);
			video.put("description", params[3]);
			jsonMap.put("video", video);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[视频]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	},

	/**
	 * 音乐
	 */
	music {

		/**
		 * 
		 * 方法名称：send <br>
		 * 描述： <br>
		 * 作者：ruibo <br>
		 * 修改日期：2017年3月5日-下午5:52:48 <br/>
		 * 
		 * @see com.zyht.wechat.message.CustomerMessageSender#send(java.lang.String,
		 *      java.lang.String, java.lang.String[])
		 * @param openid
		 * @param url
		 * @param params
		 */
		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "music");
			Map<String, Object> music = new HashMap<String, Object>();
			music.put("title", params[0]);
			music.put("description", params[1]);
			music.put("musicurl", params[2]);
			music.put("hqmusicurl", params[3]);
			music.put("thumb_media_id", params[4]);
			jsonMap.put("music", music);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[音乐]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	},

	/**
	 * 卡券
	 */
	wxcard {

		@Override
		public void send(String openid, String url, String... params) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("touser", openid);
			jsonMap.put("msgtype", "text");
			Map<String, Object> wxcard = new HashMap<String, Object>();
			wxcard.put("card_id", params[0]);
			Map<String, Object> cardExt = new HashMap<String, Object>();
			cardExt.put("code", params[1]);
			cardExt.put("timestamp", params[2]);
			cardExt.put("signature", params[3]);
			wxcard.put("card_ext", cardExt);
			jsonMap.put("wxcard", wxcard);
			try {
				String resultStr = HttpUtils.post(url,
						JSON.toJSONString(jsonMap));
				Logger logger = LoggerFactory
						.getLogger(CustomerMessageSender.class);
				if (logger.isInfoEnabled()) {
					logger.info("发送客服消息[卡券]结果是：" + resultStr);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	};

	/**
	 * 方法名称: send<br/>
	 * 描述：发送客服消息<br/>
	 * 作者: ruibo<br/>
	 * 修改日期：2017年3月5日-下午5:49:44<br/>
	 * 
	 * @param openid
	 * @param url
	 * @param params
	 */
	public abstract void send(String openid, String url, String... params);

	/**
	 * 图文，内容跳转外链
	 */
	public static void sendArticleWithRedirectUrl(String openid, String url,
			List<Article> articles) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("touser", openid);
		jsonMap.put("msgtype", "mpnews");
		Map<String, Object> news = new HashMap<String, Object>();
		news.put("articles", articles);
		jsonMap.put("news", news);
		try {
			String resultStr = HttpUtils.post(url,
					JSON.toJSONString(jsonMap));
			Logger logger = LoggerFactory
					.getLogger(CustomerMessageSender.class);
			if (logger.isInfoEnabled()) {
				logger.info("发送客服消息[图文外链]结果是：" + resultStr);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
